package com.mall.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 应用启动信息日志记录器
 * 在应用启动完成后，按企业标准格式打印服务名称和端口
 * 仅在非WebFlux应用中生效
 */
@Slf4j
@Component
public class StartupInfoLogger implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private Environment environment;

    @Value("${server.port:8080}")
    private int serverPort;

    @Value("${spring.application.name:unknown}")
    private String applicationName;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        try {
            // 确保应用上下文已经完全准备好
            ConfigurableApplicationContext context = event.getApplicationContext();
            
            // 按企业标准格式打印启动信息
            String serviceName = getServiceDisplayName(applicationName);
            String startupInfo = String.format(
                    "\n%s\n" +
                    "----------------------------------------------------------\n" +
                    "服务名称: %s\n" +
                    "服务端口: %d\n" +
                    "服务标识: %s\n" +
                    "启动时间: %s\n" +
                    "----------------------------------------------------------",
                    getBanner(),
                    serviceName,
                    serverPort,
                    applicationName,
                    java.time.LocalDateTime.now().toString()
            );
            
            // 使用日志记录启动信息
            log.info(startupInfo);
        } catch (Exception e) {
            // 使用日志记录异常信息
            log.error("启动信息打印失败: {}", e.getMessage(), e);
        }
    }

    /**
     * 根据服务标识获取服务显示名称
     *
     * @param applicationName 服务标识
     * @return 服务显示名称
     */
    private String getServiceDisplayName(String applicationName) {
        switch (applicationName) {
            case "mall-auth":
                return "认证服务";
            case "mall-system":
                return "系统服务";
            case "mall-gateway":
                return "网关服务";
            case "mall-monitor":
                return "监控服务";
            default:
                return applicationName;
        }
    }

    /**
     * 获取服务横幅
     *
     * @return 横幅字符串
     */
    private String getBanner() {
        return "  __  __       _ _   ____                  _           \n" +
               " |  \\/  | __ _| | | / ___|  ___ _ ____   _(_) ___ ___ \n" +
               " | |\\/| |/ _` | | | \\___ \\ / _ \\ '__\\ \\ / / |/ __/ _ \\\n" +
               " | |  | | (_| | | |  ___) |  __/ |   \\ V /| | (_|  __/\n" +
               " |_|  |_|\\__,_|_|_| |____/ \\___|_|    \\_/ |_|\\___\\___|\n";
    }
}